﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


public partial class _Default : System.Web.UI.Page 
{
    int cityno,userno;
    public string titletext;

    protected void Page_Load(object sender, EventArgs e)
    {

        ctpDataContext db=new ctpDataContext();


        cityno = int.Parse(Request["cno"]);
        userno = int.Parse(Request["no"]);

        String user = Request["user"];


        //this.WebPartManager1.DisplayMode = WebPartManager.BrowseDisplayMode;
        bool userEditable = false;

        this.Panel1.Visible = false;
        this.lbtn_catalog.Visible = false;
        if (Session["userno"] != null)
        {

            if ((int)Session["userno"] == userno)
            {
                userEditable = true;
                this.WebPartManager1.DisplayMode = WebPartManager.DesignDisplayMode;
                this.lbtn_catalog.Visible = true;

                this.LinkButton1.Enabled = false;

                this.Panel1.Visible = true;
            }
        }
        //if (this.WebPartManager1.DisplayMode != WebPartManager.BrowseDisplayMode)
        //{

        //}

        foreach (WebPartZone wpz in WebPartManager1.Zones)
        {
            WebPartUtil.SetWebPartZone(wpz, userEditable);
        }

   
        if (!this.IsPostBack)
        {

            var userdata = from p in db.users
                           where p.no == userno
                           select p;

            var citydata = from p in db.ct_profiles
                           where p.no == cityno
                           select p;

            ct_profile myctp = citydata.First();
            user myuser = userdata.First();


            if (myuser.nickname.Trim().Substring(myuser.nickname.Trim().Length - 1, 1) == "s")
            {
                this.Label1.Text = myuser.nickname.Trim() + "' ";
            }
            else
            {

                this.Label1.Text = myuser.nickname.Trim() + "'s ";
            }

            titletext = "Citiport - "+ this.Label1.Text+" "+myctp.title.Trim();
            this.Label2.Text = myctp.title.Trim();

            this.HyperLink2.NavigateUrl = "index.aspx?no=" + userno.ToString();
            this.HyperLink2.Text = "back to " + this.Label1.Text + " Citiport";

            this.HyperLink3.Text = "goto Citiport " + this.Label2.Text.Trim();
            this.HyperLink3.NavigateUrl = "../city.aspx?no=" + cityno.ToString();

            this.HyperLink4.Text = "Citiport " + this.Label2.Text;
            this.HyperLink4.NavigateUrl = "../city.aspx?no=" + cityno.ToString();


        }

        if (Request["sub"] == "1") this.subcribe();
    }

    void subcribe()
    {
        if (Session["userno"] == null) return;
        if ((int)Session["userno"] == userno) return;

        this.Label3.Text = "Subscribing...";
        this.Label3.Visible = true;

        ctpDataContext db = new ctpDataContext();

        my_citiport_mine cdata = db.my_citiport_mines.Single(p => p.userno == int.Parse(Request["no"]) && p.ct_no == int.Parse(Request["cno"]));

        int newno = cdata.no;

        int check = db.my_citiport_subscribes.Count(p => p.userno == (int)Session["userno"] && p.subscribe_ct_no == newno);
        if (check != 0)
        {
            this.Label3.Text = "You have subscribed this city.";
            return;
        }



        my_citiport_subscribe my = new my_citiport_subscribe();
        my.addtime = DateTime.Now;
        my.userno = (int)Session["userno"];
        my.subscribe_ct_no = newno;

        db.my_citiport_subscribes.InsertOnSubmit(my);
        db.SubmitChanges();

        my_citiport_mine mine = db.my_citiport_mines.Single(p => p.userno == userno && p.ct_no == cityno);
        mine.recommend += 1;
        db.SubmitChanges();

        this.Label3.Text = "Successfully subscribed";
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if (Session["userno"] == null)
        {
            ctp_lib.check_login(this.Page, "my/default.aspx?no=" + userno.ToString() + "&cno=" + cityno.ToString() + "&sub=1",false);
        }
        else
        {
            this.subcribe();
        }


    }

    protected void lbtn_catalog_Click(object sender, EventArgs e)
    {
      // this.Label3.Text += "Edit Mode";
        this.WebPartManager1.DisplayMode = WebPartManager.CatalogDisplayMode;
        
        //foreach (WebPartZone wpz in WebPartManager1.Zones)
        //{
        //    WebPartUtil.SetWebPartZone(wpz, true);
        //}
    }


    protected void WebPartManager1_DisplayModeChanged(object sender, WebPartDisplayModeEventArgs e)
    {
        
        if (this.WebPartManager1.DisplayMode == WebPartManager.BrowseDisplayMode)
        {
            this.WebPartManager1.DisplayMode = WebPartManager.DesignDisplayMode;
        }
    }

}
